/*-+-*****************************************************************************
 Copyright (C) 2009-2012 Oceanside Software Corporation (R)
 Dallas, Texas

 Author       : Jason T. Brower
 Purpose      : This file is part of a point-of-sale software package
                originally written for Table-Service Restaurants.
              
 Date         : July 8th of 2012
              
 History      : The application that this software file belongs to was 
                written over a 3-Year period.  The first version was written
                in C# .Net 3.5 using Windows Forms.  Being new to .Net at
                the time, I soon learned of Windows Presentation Foundation,
                liked the graphics capabilities and databinding so I
                migrated to WPF.  When the .Net 4.0 framework was released
                the application was migrated from 3.5 to 4.0. Now today,
                .Net 4.5 RC is out and I was already in process of migrating
                before I made this public.
                
Excuse Me     : Please excuse the coding style, it is not representative of
                my typical work.  During the three years I was under a very
                tight budget, constantly running out of funding and was 
                working 18 hour days hyped up on 5-Hour Energy.  The 
                design is not textbook.  It is monolythic because of
                previous issues I will not get into.  There are few comments
                in the code and the ones that are there may be incorrect.
                
What it is not: This is not the exact version that I took through PA-DSS 2.0
                nor is it the exact version running at my customer sites.
                I removed some details for security purposes including some
                encryption algorithms.  The reporting was removed because
                I was including a third party library that cannot be 
                published with this Open Source.
                
What it is    : This application has been running at high volume restaraunts
                for over a year.  It never freezes, crashes or does anything
                unexpected.  So long as it is built, installed and configured
                correctly, it is a strong system that we can all enhance.

Contact       : Please note that I can no longer work for free.  However, you
                can reach me at information@oceansidesw.com.                                                      

*********************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
namespace Oceanside
{
    public class CFD
    {
        private static bool _isInit = false;

        private static string _portSemaphore = "";
        private static SerialPort _sp = null;

        private const string CLRCFD = "\x0C";
        private const string MOVERIGHTMOST = "\x1F\x0D";
        private const string MOVELEFTONE = "\x08";
        private const string MOVCURSORHOMETOP = "\x1F\x24\x01\x01";
        private const string MOVCURSORHOMEBOTTOM = "\x1F\x24\x01\x02";
        private const string BLINK = "\x1F\x45\x0F";
        private const string NOBLINK = "\x1F\x45\x00";

        public static void Init()
        {
            try
            {
                if (Constants.HasCfd)
                {
                    _sp = new SerialPort(Constants.CfdCom, 9600, Parity.None, 8, StopBits.One);
                    _sp.Open();
                    _isInit = true;
                    WriteCustomStrings();
                }
            }
            catch (Exception e)
            {
                Logging.LogException(e);
            }
        }

        public static void WriteCustomStrings()
        {
            if (Constants.HasCfd)
            {
                lock (_portSemaphore)
                {
                    if (!_isInit)
                    {
                        Init();
                        _isInit = true;
                    }
                    _sp.Write(NOBLINK);
                    _sp.Write(CLRCFD);
                    _sp.Write(MOVCURSORHOMETOP);
                    _sp.Write(Constants.CfdLine1);
                    _sp.Write(MOVCURSORHOMEBOTTOM);
                    _sp.WriteLine(Constants.CfdLine2);
                }
            }
        }

        public static void WriteAmount(decimal amountDue, bool isChange)
        {
            try
            {
                if (Constants.HasCfd)
                {
                    lock (_portSemaphore)
                    {
                        if (!_isInit)
                        {
                            Init();
                            _isInit = true;
                        }

                        if (!isChange)
                        {
                            _sp.Write(CLRCFD);
                            _sp.Write(MOVCURSORHOMETOP);
                            _sp.Write(NOBLINK);
                            _sp.Write("Due: " + amountDue.ToString("c"));
                        }
                        else
                        {
                            _sp.Write(CLRCFD);
                            _sp.Write(MOVCURSORHOMETOP);
                            _sp.Write(BLINK);
                            _sp.Write("Change: " + amountDue.ToString("c"));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logging.LogException(ex);
            }
        }
    }
}
